var $;
layui.config({
    base : "js/"
}).use(['form','layer','jquery','laypage'],function(){
    var form = layui.form(),
        layer = parent.layer === undefined ? layui.layer : parent.layer,
        laypage = layui.laypage;
    $ = layui.jquery;

    $.ajax({
        url : "http://localhost/ci/admin/role/imp_edit",
        type : "post",
        data : {'role_id':parent.$('.ci_data').val(), 'token':localStorage.token,'user_token':localStorage.user},
        dataType : "json",
        success : function(data){
            console.log(data['role']);
            $('.roleName').val(data['role'].role_name);
            $('.remark').val(data['role'].remark);
            $('.date').val(data['role'].create_time);
            //top.layer.msg("用户回显成功！");
        },
        error : function (data) {
            console.log(data);
            //top.layer.msg("用户回显失败！");
        }
    })


    var addUserArray = [],updateUser = [];
    form.on("submit(updateRole)",function(data){
        //是否添加过信息
        /*if(window.sessionStorage.getItem("addUser")){
         addUserArray = JSON.parse(window.sessionStorage.getItem("addUser"));
         }*/


        updateRole = '{"role_id":"'+parent.$('.ci_data').val()+'","role_name":"'+ $('.roleName').val() + '","remark":"'+ $('.remark').val() +'"}';

        $.ajax({
            url : "http://localhost/ci/admin/role/imp_update",
            type : "post",
            data : {'data':updateRole, 'token':localStorage.token,'user_token':localStorage.user},
            dataType : "json",
            success : function(data){
                console.log(data);
                //top.layer.close(index);
                top.layer.msg("用户修改成功！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            },
            error : function (data) {
                console.log(data);
                //top.layer.close(index);
                top.layer.msg("用户修改失败！");
                layer.closeAll("iframe");
                //刷新父页面
                parent.location.reload();
            }
        })
        return false;
    })

})

//格式化时间
function formatTime(_time){
    var year = _time.getFullYear();
    var month = _time.getMonth()+1<10 ? "0"+(_time.getMonth()+1) : _time.getMonth()+1;
    var day = _time.getDate()<10 ? "0"+_time.getDate() : _time.getDate();
    var hour = _time.getHours()<10 ? "0"+_time.getHours() : _time.getHours();
    var minute = _time.getMinutes()<10 ? "0"+_time.getMinutes() : _time.getMinutes();
    return year+"-"+month+"-"+day+" "+hour+":"+minute;
}
